package com.bdsy.business.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bdsy.api.annotation.IgnoreAuth;
import com.bdsy.business.model.Order;
import com.bdsy.business.model.OrderDetails;
import com.bdsy.business.model.ShopType;
import com.bdsy.business.service.OrderDetailsService;
import com.bdsy.business.service.OrderService;
import com.bdsy.business.service.ShopCarService;
import com.bdsy.business.service.ShopTypeService;
import com.bdsy.common.annotation.Json;
import com.bdsy.common.utils.DateRandomUtil;
import com.bdsy.common.utils.Result;
import com.bdsy.system.annotation.SysLog;



/**
 * 订单信息
 * @author MING
 * @date 2018年1月4日 下午4:02:16
 */

@Controller
@RequestMapping(value = {"${web.adminPath}", "${web.appPath}"})
public class OrderController {

	@Autowired
	OrderService orderService;
	
	@Autowired
	OrderDetailsService orderDetailsService;
	
	@Autowired
	ShopCarService shopCarService;

	@Autowired
	ShopTypeService shopTypeService;
	
	
	/**
	 * 查询当前用户订单信息
	 */
	@Json
	@RequestMapping(value = "orderById")
	public Object orderById(HttpServletRequest request,String userId) {
		userId = request.getParameter("userId");
		Map<String,Object> map = new LinkedHashMap<>();
		@SuppressWarnings("rawtypes")
		List<Map> orderList = orderService.selectOrderById(userId);
		map.put("code", 200);
		map.put("msg", "success");
		map.put("data", orderList);
		return map;
	}
	
	
	/**
	 * 查询订单信息
	 */
	@Json
	@RequestMapping(value = "selectOrderById")
	public Object selectOrderById(@Param("userId")String userId,@Param("orderStatus")int status) {
		Map<String,Object> map = new LinkedHashMap<>();
		@SuppressWarnings("rawtypes")
		List<Map> orderList = orderService.selectOrderByIdStatus(userId, status);
		map.put("code", 200);
		map.put("msg", "success");
		map.put("data", orderList);
		return map;
	}
	
	
	/**
	 * 查询订单信息 （用户ID 状态 可为空） ------未使用 -----  
	 */
	
	@Json
	@RequestMapping(value = "selectOrderList")
	public Object selectOrderList(String userId,int status) {
		Map<String,Object> map = new LinkedHashMap<>();
		Map<String,Object> map2 = null;
		Map<String,Object> map3 = new LinkedHashMap<>();
		@SuppressWarnings("rawtypes")
		List<Map> list  = new ArrayList<>();
		map.put("code", 200);
		map.put("msg", "success");
		//获得所有订单ID
		List<String> orderIdList =  orderService.selectOrderId(userId,status);
		//根据订单ID 查询订单信息
		for (int i = 0; i < orderIdList.size(); i++) {
	/**
	 * selectOrderByOrderId(orderIdList.get(i),0);
	 * 0 有问题 暂时存放  缺少参数
	 */
			@SuppressWarnings("rawtypes")
			List<Map> orderList  = orderService.selectOrderByOrderId(orderIdList.get(i),0);
			map2 = new LinkedHashMap<>();
			map2.put("orderId"+i, orderIdList.get(i));
			orderList.add(map2);
			map3.put("orderList"+i, orderList);
		}
		list.add(map3);
		map.put("data", list);
		return map;
	}
	
	
	/**
	 * 根据用户、状态 查询当前用户订单信息(只有订单)
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "orderListById")
	public Object orderListById(HttpServletRequest request,String userId,int orderStatus,int status) {
		Map<String,Object> map = new LinkedHashMap<>();
		List<Map> orderList = orderService.orderListById(userId,orderStatus,status);
		map.put("code", 200);
		map.put("msg", "success");
		map.put("data", orderList);
		return map;
	}
	
	/**
	 * 根据订单编号查询订单信息
	 */
	@Json
	@RequestMapping(value = "selectOrderByOrderId")
	public Object selectOrderByOrderId(HttpServletRequest request,String orderId,int status) {
		Map<String,Object> map = new LinkedHashMap<>();
		List<Map> orderList = orderService.selectOrderByOrderId(orderId,status);
		map.put("code", 200);
		map.put("msg", "success");
		map.put("data", orderList);
		return map;
	}
	
	// 查询订单信息  未发货 未收货 已完成 
	/**
	 * 根据用户编号状态查询订单信息
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "orderListByIdStatus")
	public Object orderListByIdStatus(String userId,int status) {
		Map<String,Object> map = new LinkedHashMap<>();
		List<Map> orderList = orderService.orderListByIdStatus(userId, status);
		map.put("code", 200);
		map.put("msg", "success");
		map.put("data", orderList);
		return map;
	}
	
	
	
	
	/**
	 * 添加订单信息
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "addOrder")
	public Object addOrder(Order order,OrderDetails orderDetails) {
		//收货地址reId 用户id ?
		String num = DateRandomUtil.getRandomFileName();
		order.setOrderId(num);
		boolean result = orderService.add(order);
		//订单详情
		orderDetails.setOrderId(num);
		orderDetails.setStatus(0);
		boolean result2 = orderDetailsService.add(orderDetails);
		if (result && result2) {
			return Result.ok("addSuccess");
		} else {
			return Result.error("addFail");
		}
	}
	
	
	/**
	 * 修改订单状态 传入status状态值
	 */
	@Json
	@RequestMapping(value = "updateOrderStatus")
	public Object updateOrderStatus(Order order,int status) {
		boolean result = orderService.updateOrderStatus(order, status);
		if (result) {
			return Result.ok("updateSuccess");
		} else {
			return Result.error("updateFail");
		}
	}
	
	/**
	 * 修改订单详情状态
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "updateOrderDetailsStatus")
	public Object updateOrderDetailsStatus(OrderDetails orderDetails) {
		boolean result = orderDetailsService.updateOrderDetailStatus(orderDetails);
		if (result) {						 
			return Result.ok("updateSuccess");
		} else {
			return Result.error("updateFail");
		}
	}
	
	
	
	/**
	 * 购物车生成订单信息
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "addOrderInfo")
	public Object addOrderInfo(Order order,HttpServletRequest request,String ids) {
		String or = request.getParameter("or");
		List<OrderDetails> list = new ArrayList<OrderDetails>();  
	    list = JSONObject.parseArray(or, OrderDetails.class);  
		//收货地址reId 用户id ?
		String num = DateRandomUtil.getRandomFileName();
		order.setOrderId(num);
		boolean result = orderService.add(order);
		//订单详情
		for (OrderDetails orderDetail : list) {
			orderDetail.setOrderId(num);
		}
		boolean result2 = orderDetailsService.insertOrderDetails(list);
		//清除购物车内的商品
		List<String> lis = JSON.parseArray(ids, String.class);
		boolean result3 = shopCarService.deleteShopCar(lis);
		
		if (result && result2 && result3) {
			return Result.ok("addSuccess");
		} else {
			return Result.error("addFail");
		}
	}
	
	
	/**
	 * 删除当前用户订单信息 
	 */
	@IgnoreAuth
	@Json
	@RequestMapping(value = "deleteOrderById")
	public Object deleteOrderById(String ids) {
		List<String> list = JSON.parseArray(ids, String.class);
		boolean result = orderService.deleteOrderById(list);
		boolean result2 = orderDetailsService.deleteOrderDetailsById(list);
		if (result && result2) {
			return Result.ok("deleteSuccess");
		} else {
			return Result.error("deleteFail");
		}
	}
	
	
	/**
	 * 根据用户查询未完成订单信息
	 */
	@Json
	@RequestMapping(value = "selectNOrderById")
	public Object selectNOrderById(String userId) {
		Map<String,Object> map = new LinkedHashMap<>();
		@SuppressWarnings("rawtypes")
		List<Map> nOrderList = orderService.selectNOrderById(userId);
		map.put("code", 200);
		map.put("msg", "success");
		map.put("data", nOrderList);
		return map;
	}
	
	
	//后台
	@RequestMapping(value = "orderManager")
	public String orderManager(Model model) {
		List<ShopType> shopTypeInfo = shopTypeService.shopTypeInfo();
			if (!shopTypeInfo.isEmpty()) {
				model.addAttribute("shopTypeInfo", shopTypeInfo);
			}
		return "admin/business/orderManager";
	}
	@RequestMapping(value = "zorderManager")
	public String zorderManager(Model model) {
		List<ShopType> shopTypeInfo = shopTypeService.shopTypeInfo();
		if (!shopTypeInfo.isEmpty()) {
			model.addAttribute("shopTypeInfo", shopTypeInfo);
		}
		return "admin/business/zorderManager";
	}
	
	
	
	@SysLog("当前用户 出售 商品的状态（付款 发货 收货 完成） ")
	@RequestMapping(value = "orderListByUserIdStatus")
	@ResponseBody
	public Object orderListByUserIdStatus(@RequestParam(value="userId",required=false)String userId,@RequestParam(value="shopTypeId",required=false)int shopTypeId,int orderStatus,int status){
		@SuppressWarnings("rawtypes")
		List<Map> orderList = orderService.orderListByUserIdStatus(userId, shopTypeId, orderStatus, status);
		return orderList;
	}
	
	
	
	@SysLog("发货状态")
	@Json
	@RequestMapping(value = "updateOrderDetailStatus")
	public Object updateOrderDetailStatus(OrderDetails orderDetails) {
		boolean result = orderDetailsService.updateOrderDetailStatus(orderDetails);
		if (result) {
    		return Result.ok("发货成功");
		}else{
			return Result.error("发货失败");
		}
	}
	
	
}
